﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IO = System.IO;

namespace SignLockerTest
{
    public partial class SavedFileForm : Form
    {
        public SavedFileForm()
        {
            InitializeComponent();
        }

        public void Add(string fileName)
        {
            DownloadedFile info = new DownloadedFile() { File = fileName };
            int rowIdx = dataGridView1.Rows.Add(dataGridView1.Rows.Count + 1, fileName, DateTime.Now, "Open");
            dataGridView1.Rows[rowIdx].Tag = info;
        }

        private void SavedFileForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Visible = false;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["colOpen"].Index)
            {
                DownloadedFile file = (DownloadedFile)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Tag;
                file.Open();
            }
        }
    }

    class DownloadedFile
    {
        public string File { get; set; }
        public object Tag { get; set; }

        public void Open()
        {
            if (!IO.File.Exists(File)) return;
            System.Diagnostics.Process.Start(File);
        }
    }

    class OpenButton : Button
    {
        private DownloadedFile _file;
        public OpenButton(DownloadedFile file)
            : base()
        {
            Text = "Open";
            _file = file;
        }

        protected override void OnClick(EventArgs e)
        {
            _file.Open();
        }
    }
}
